﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HRM.Models;
using System.Data.SqlClient;
namespace HRM.Controllers.DangKy
{
    public class DangKy_HDHocThuatController : Controller
    {
        private HRMTTNS41Context db = new HRMTTNS41Context();

        //
        // GET: /DangKy_HDHocThuat/

        public ActionResult Index()
        {
            var dk_hdhocthuat = db.dk_HDHocThuat;
            return View(dk_hdhocthuat.ToList());
        }

        //
        // GET: /DangKy_HDHocThuat/Details/5

        public ActionResult Details(int id = 0)
        {
            dk_HDHocThuat dk_hdhocthuat = db.dk_HDHocThuat.Find(id);
            if (dk_hdhocthuat == null)
            {
                return HttpNotFound();
            }
            return View(dk_hdhocthuat);
        }

        //
        // GET: /DangKy_HDHocThuat/Create

        public ActionResult Create()
        {
            ViewBag.MaDDKy = new SelectList(db.DotDK_TK, "MaDDKy", "TenDDKy");
            //ViewBag.MaNV = new SelectList(db.NhanViens, "MaNV", "HoTen");
            return View();
        }

        //
        // POST: /DangKy_HDHocThuat/Create

        [HttpPost]
        public ActionResult Create(dk_HDHocThuat dk_hdhocthuat,string id)
        {
            if (ModelState.IsValid)
            {
                if (id == "chutri")
                {
                    SqlConnection sqlConn = new SqlConnection("Data Source=BIBI-PC;Initial Catalog=HRMTTNS41;Persist Security Info=True;User ID=sa;Password=123456;MultipleActiveResultSets=True");
                    SqlCommand sqlCommand = new SqlCommand("insert into dk_HDHocThuat(SoLuong,MaDDKy,MaDKHDHT) values('" + dk_hdhocthuat.SoLuong + "','" + dk_hdhocthuat.MaDDKy + "','" + "DkHdht11" + "')", sqlConn);
                    // TempData["Message"] = "Xóa thông báo thành công";
                    sqlConn.Open();
                    sqlCommand.ExecuteNonQuery();
                    // db.dk_HDHocThuat.Add(dk_hdhocthuat);
                    //db.SaveChanges();
                }
                else
                {
                    SqlConnection sqlConn = new SqlConnection("Data Source=BIBI-PC;Initial Catalog=HRMTTNS41;Persist Security Info=True;User ID=sa;Password=123456;MultipleActiveResultSets=True");
                    SqlCommand sqlCommand = new SqlCommand("insert into dk_HDHocThuat(SoLuong,MaDDKy,MaDKHDHT) values('" + dk_hdhocthuat.SoLuong + "','" + dk_hdhocthuat.MaDDKy + "','" + "DkHdht12" + "')", sqlConn);
                    // TempData["Message"] = "Xóa thông báo thành công";
                    sqlConn.Open();
                    sqlCommand.ExecuteNonQuery();
                    // db.dk_HDHocThuat.Add(dk_hdhocthuat);
                    //db.SaveChanges();
                }
                return RedirectToAction("Index");

            }

            ViewBag.MaDDKy = new SelectList(db.DotDK_TK, "MaDDKy", "TenDDKy", dk_hdhocthuat.MaDDKy);
            //ViewBag.MaNV = new SelectList(db.NhanViens, "MaNV", "HoTen", dk_hdhocthuat.MaNV);
            return View(dk_hdhocthuat);
        }

        //
        // GET: /DangKy_HDHocThuat/Edit/5

        public ActionResult Edit(int id = 0)
        {
            dk_HDHocThuat dk_hdhocthuat = db.dk_HDHocThuat.Find(id);
            if (dk_hdhocthuat == null)
            {
                return HttpNotFound();
            }
            ViewBag.MaDDKy = new SelectList(db.DotDK_TK, "MaDDKy", "TenDDKy", dk_hdhocthuat.MaDDKy);
           // ViewBag.MaNV = new SelectList(db.NhanViens, "MaNV", "HoTen", dk_hdhocthuat.MaNV);
            return View(dk_hdhocthuat);
        }

        //
        // POST: /DangKy_HDHocThuat/Edit/5

        [HttpPost]
        public ActionResult Edit(dk_HDHocThuat dk_hdhocthuat)
        {
            if (ModelState.IsValid)
            {
                db.Entry(dk_hdhocthuat).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.MaDDKy = new SelectList(db.DotDK_TK, "MaDDKy", "TenDDKy", dk_hdhocthuat.MaDDKy);
            //ViewBag.MaNV = new SelectList(db.NhanViens, "MaNV", "HoTen", dk_hdhocthuat.MaNV);
            return View(dk_hdhocthuat);
        }

        //
        // GET: /DangKy_HDHocThuat/Delete/5

        public ActionResult Delete(int id = 0)
        {
            dk_HDHocThuat dk_hdhocthuat = db.dk_HDHocThuat.Find(id);
            if (dk_hdhocthuat == null)
            {
                return HttpNotFound();
            }
            return View(dk_hdhocthuat);
        }

        //
        // POST: /DangKy_HDHocThuat/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            dk_HDHocThuat dk_hdhocthuat = db.dk_HDHocThuat.Find(id);
            db.dk_HDHocThuat.Remove(dk_hdhocthuat);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}